from peewee import *

db = MySQLDatabase('test',
                   **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'user': 'root',
                      'password': 'root'})


class UnknownField(object):
    def __init__(self, *_, **__): pass


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    age = IntegerField(null=True)
    birthday = DateTimeField(null=True)
    name = CharField(null=True)

    class Meta:
        table_name = 'user'


# 批量插入元组
from random import randint

data = [
    (f'赵六{i}号', randint(20, 40), f'{randint(1980, 2010)}-{randint(1, 13)}-{randint(1, 29)}')
    for i in range(10000)
]

from peewee import chunked

# Insert rows 100 at a time.
# chunked辅助函数实现每次插入100条数据
with db.atomic():
    for batch in chunked(data, 100):
        User.insert_many(batch, fields=('name', 'age', 'birthday')).execute()
